using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Media3D;
using _3DHouse.CustomControl;

namespace _3DHouse.Lib
{
    /// <summary>
    /// 拉伸的撤消重做控制类
    /// </summary>
    class ResizeCommand : ICommand
    {
       // private ScaleTransform3D _Scale;
       // private double _ChangeofWidth;//x改变的大小
       // private double _Changeofheight;//z改变的大小
       // double _Z;
        private Cube _UiElement;//当前对象
        Cube cloneModel;//保存旧数据
        Cube newModel;//保存新数据
        //public ResizeCommand( double x,double y,double z, Cube uiElement)
        //{           
        //    _ChangeofWidth = x;
        //    _Changeofheight = y;
        //    _Z = z;
        //    _UiElement = uiElement;
           
        //    //原来的改变
        //   // GradualScale(x, z);
        //}
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="oldMode">保存旧数据的对象</param>
        /// <param name="uiElement">当前对象</param>
        public ResizeCommand(Cube oldMode,Cube uiElement)
        {
            //克隆旧数据
            cloneModel =new Cube();
            cloneModel.Content = oldMode.Content.Clone();           
            foreach (Visual3D visual in oldMode.Children)
            {
                if (visual is Viewport2DVisual3D)
                {
                    Viewport2DVisual3D visual2d = visual as Viewport2DVisual3D;                    
                    Viewport2DVisual3D visualNew = new Viewport2DVisual3D();
                    visualNew.Geometry = visual2d.Geometry.Clone();
                    cloneModel.Children.Add(visualNew);
                }
            }
            //克隆新数据
            newModel = new Cube();
            newModel.Content = uiElement.Content.Clone();
            foreach (Visual3D visual in uiElement.Children)
            {
                if (visual is Viewport2DVisual3D)
                {
                    Viewport2DVisual3D visual2d = visual as Viewport2DVisual3D;
                    Viewport2DVisual3D visualNew = new Viewport2DVisual3D();
                    visualNew.Geometry = visual2d.Geometry.Clone();
                    newModel.Children.Add(visualNew);
                }
            }
            _UiElement = uiElement;          
        }
        #region ICommand Members

        public void Execute()
        {
         /*
            Transform3DGroup tfg = _UiElement.Transform as Transform3DGroup;
            foreach (Transform3D tr in tfg.Children)
            {
                if (tr.GetType().Name.Equals("ScaleTransform3D"))
                {
                    ScaleTransform3D scale = tr as ScaleTransform3D;
                    scale.ScaleX += _ChangeofWidth;
                    scale.ScaleZ +=  _Z;
                    scale.ScaleY += _Changeofheight;                  
                }

            }
            _UiElement.Transform = tfg;*/
            //重做回到前一半坐标
            if (newModel != null && newModel.Content is Model3DGroup)
            {
                Model3DGroup model3DGroup = _UiElement.Content as Model3DGroup;//当前对象的图形集合
                Model3DGroup newModel3DGroup = newModel.Content as Model3DGroup;//新对象的图形集合
                for (int n = 0; n < model3DGroup.Children.Count; n++)
                {
                    GeometryModel3D newGeome = newModel3DGroup.Children[n] as GeometryModel3D;
                    MeshGeometry3D newMesh = newGeome.Geometry as MeshGeometry3D;
                    GeometryModel3D geome = model3DGroup.Children[n] as GeometryModel3D;
                    MeshGeometry3D mesh = geome.Geometry as MeshGeometry3D;
                    mesh.Positions = newMesh.Positions;//把新坐标给当前模型
                }
            } if (newModel != null && newModel.Content is GeometryModel3D)
            {
                GeometryModel3D cloneGeome = newModel.Content as GeometryModel3D;
                MeshGeometry3D cloneMesh = cloneGeome.Geometry as MeshGeometry3D;
                GeometryModel3D geome = _UiElement.Content as GeometryModel3D;
                MeshGeometry3D mesh = geome.Geometry as MeshGeometry3D;
                mesh.Positions = cloneMesh.Positions;//把旧坐标给当前模型
            }
            //遍历子元素，主要是viewport2dVisua3d        
            if (newModel != null && newModel.Children.Count > 0)
            {
                for (int m = 0; m < newModel.Children.Count; m++)
                {
                    Visual3D visual = newModel.Children[m] as Visual3D;
                    if (visual is Viewport2DVisual3D)
                    {
                        Viewport2DVisual3D cloneGeome = visual as Viewport2DVisual3D;
                        MeshGeometry3D cloneMesh = cloneGeome.Geometry as MeshGeometry3D;
                        Viewport2DVisual3D visual2d = _UiElement.Children[m] as Viewport2DVisual3D;
                        MeshGeometry3D mesh = visual2d.Geometry as MeshGeometry3D;
                        mesh.Positions = cloneMesh.Positions;//把旧坐标给当前模型
                    }
                }
            }
        }

        public void UnExecute()
        {
            //_Scale = new ScaleTransform3D(_Scale.ScaleX - _ChangeofWidth, 1, _Scale.ScaleZ - _Changeofheight);
            //Transform3DGroup tfg = _UiElement.Transform as Transform3DGroup;  
            //foreach (Transform3D tr in tfg.Children)
           // {
                //if (tr.GetType().Name.Equals("ScaleTransform3D"))
                //{
                    //ScaleTransform3D scale = tr as ScaleTransform3D;
                   // scale.ScaleX = 1 / (1 + _ChangeofWidth);//精度不对，
                   // scale.ScaleZ = 1 / (1 + _Z);
                   // scale.ScaleY = 1 / (1 + _Changeofheight);  
                    /*//设置撤消控制变量，撤消时进行除法计算
                     _UiElement.cheXiao = true;
                    scale.ScaleX += _ChangeofWidth;
                    _UiElement.cheXiao = false;*/
                    //从克隆里获取坐标                    
                //}
            //}
           // _UiElement.Transform = tfg;  
            /*_UiElement.Height = _UiElement.Height - _Changeofheight;
            _UiElement.Width = _UiElement.Width - _ChangeofWidth;
            _UiElement.Margin = new Thickness(_UiElement.Margin.Left -
        _ChangeOfMargin.Left, _UiElement.Margin.Top -
            _ChangeOfMargin.Top, _UiElement.Margin.Right -
        _ChangeOfMargin.Right, _UiElement.Margin.Bottom -
        _ChangeOfMargin.Bottom);*/
            //ScaleTransform3D scale = new ScaleTransform3D();
            //scale.ScaleX -= _ChangeofWidth;
            //scale.ScaleZ -= _Changeofheight;
            //_UiElement.Transform = scale;
            /*回到上一步的坐标*/
            if (cloneModel != null && cloneModel.Content is Model3DGroup)
            {
                Model3DGroup model3DGroup = _UiElement.Content as Model3DGroup;
                Model3DGroup cloneModel3DGroup = cloneModel.Content as Model3DGroup;
                for (int n = 0; n < model3DGroup.Children.Count; n++)
                {
                    GeometryModel3D cloneGeome = cloneModel3DGroup.Children[n] as GeometryModel3D;
                    MeshGeometry3D cloneMesh = cloneGeome.Geometry as MeshGeometry3D;
                    GeometryModel3D geome = model3DGroup.Children[n] as GeometryModel3D;
                    MeshGeometry3D mesh = geome.Geometry as MeshGeometry3D;
                    mesh.Positions = cloneMesh.Positions;//把旧坐标给当前模型
                }
            }
            if (cloneModel != null && cloneModel.Content is GeometryModel3D)
            {
                GeometryModel3D cloneGeome = cloneModel.Content as GeometryModel3D;
                MeshGeometry3D cloneMesh = cloneGeome.Geometry as MeshGeometry3D;
                GeometryModel3D geome = _UiElement.Content as GeometryModel3D;
                MeshGeometry3D mesh = geome.Geometry as MeshGeometry3D;
                mesh.Positions = cloneMesh.Positions;//把旧坐标给当前模型
            } 
            //遍历子元素，主要是viewport2dVisua3d        
            if (cloneModel != null && cloneModel.Children.Count > 0)
            {
                for (int m = 0; m < cloneModel.Children.Count; m++)
                {
                    Visual3D visual = cloneModel.Children[m] as Visual3D;
                    if (visual is Viewport2DVisual3D)
                    {
                        Viewport2DVisual3D cloneGeome = visual as Viewport2DVisual3D;
                        MeshGeometry3D cloneMesh = cloneGeome.Geometry as MeshGeometry3D;
                        Viewport2DVisual3D visual2d = _UiElement.Children[m] as Viewport2DVisual3D;
                        MeshGeometry3D mesh = visual2d.Geometry as MeshGeometry3D;
                        mesh.Positions = cloneMesh.Positions;//把旧坐标给当前模型
                    }
                }
            }
        }

        #endregion
        //private void GradualScale(double x,double z)
        //{
        //    if (_UiElement != null)
        //    {
        //        Transform3DGroup tfg = new Transform3DGroup();
        //        //组合变换
        //        if (_UiElement != null && _UiElement.Transform != null && _UiElement.Transform.GetType().Name.Equals("Transform3DGroup"))
        //        {
        //            tfg = _UiElement.Transform as Transform3DGroup;
                                      
        //            foreach (Transform3D tr in tfg.Children)
        //            {
        //                if (tr.GetType().Name.Equals("ScaleTransform3D"))
        //                {
        //                    _Scale = tr as ScaleTransform3D;
        //                   // _Scale.ScaleX += x;
        //                   // _Scale.ScaleZ += z;
        //                }
        //                //else if (tr.GetType().Name.Equals("TranslateTransform3D"))
        //                //{
        //                //    tran = tr as TranslateTransform3D;
        //                //}
        //                //else if (tr.GetType().Name.Equals("RotateTransform3D"))
        //                //{
        //                //    rota = tr as RotateTransform3D;
        //                //}
        //            }
        //            if (_Scale == null)
        //            {
        //               // _Scale = new ScaleTransform3D();
        //               // _Scale.ScaleX += x;
        //               // _Scale.ScaleZ += z;
        //                /*Transform3DGroup tfg2 = new Transform3DGroup();
        //                tfg2.Children.Add(_Scale);
        //                if (tran != null)
        //                {
        //                    tfg2.Children.Add(tran);
        //                }
        //                if (rota != null)
        //                {
        //                    tfg2.Children.Add(rota);
        //                }
        //                _UiElement.Transform = tfg2;*/
        //            }
        //        }
        //        else if (_UiElement != null && _UiElement.Transform != null)
        //        {//单个位移                 
        //            TranslateTransform3D tran = _UiElement.Transform as TranslateTransform3D;

        //           // _Scale.ScaleX += x;
        //           // _Scale.ScaleZ = z;
        //            //tfg.Children.Add(_Scale);
        //           // tfg.Children.Add(tran);
        //           // _UiElement.Transform = tfg;
        //        }                
        //    }
        //}
    } 
}
